import {Spu} from "../../models/spu";
import {ShoppingWay} from "../../core/enum";
import {SaleExplain} from "../../models/sale-explain";
import {getSystemSize, getWindowHeightRpx} from "../../utils/system";
import {px2rpx} from "../../miniprogram_npm/lin-ui/utils/util";


Page({
    data: {
        spu: null,
        showRealm: false,
        orderWay: null,
        specs: null,
        explain: null,
    },
    onAddToCart(event) {
        this.setData({
            showRealm: true,
            orderWay: ShoppingWay.CART
        })
    },
    onBuy(event) {
        this.setData({
            showRealm: true,
            orderWay: ShoppingWay.BUY
        })
    },
    onGotoHome(event) {
        // 跳转到tab页面
        wx.switchTab({
            url: '/pages/home/home'
        })
    },
    onGoToCart(event) {
        wx.switchTab({
            url: '/pages/cart/index'
        })
    },
    onSpecChange(event) {
        this.setData({
            specs: event.detail
        })
    },

    /**
     * 页面加载
     * @param options 页面跳转传递过来的参数
     * @returns {Promise<void>}
     */
    onLoad: async function (options) {
        const pid = options.pid
        // 获取 spu detail 详细信息
        const spu = await Spu.getDetail(pid)
        const explain = await SaleExplain.getFixed()
        const windowHeightRpx = await getWindowHeightRpx()
        const h = windowHeightRpx - 100
        // 复制到当前 data 中
        this.setData({
            spu,
            explain,
            h
        })
    }
});